﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using WcfService1.DTO;

namespace MSIACCSL2.Views.MutasiWarehouse
{
    public partial class FrmAdd : Page
    {

        public MutasiWarehouseWrapViewModel WrapViewModel
        {
            get
            {
                return this.Resources["POW"] as MutasiWarehouseWrapViewModel;
            }
        }

        public MutasiWarehouseViewModel ViewModel
        {
            get
            {
                return this.DataContext as MutasiWarehouseViewModel;
            }
        }
        public FrmAdd()
        {
            InitializeComponent();

            this.Loaded += (x,y) =>
                               {
                                   (this.Resources["POW"] as MutasiWarehouseWrapViewModel).MutasiWarehouseViewModel =
                                        (MutasiWarehouseViewModel)this.DataContext;
                               };
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void textBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                WrapViewModel.GetInventory(((TextBox)sender).DataContext);
        }

        //private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        //{
        //    WrapViewModel.MutasiWarehouseDetail = (sender as TextBox).DataContext as MutasiWarehouseDetailDTO;
        //    WrapViewModel.GetQtyUnit(null);
        //}

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	(sender as TextBox).SelectAll();
        }
     
        private void txtNoLog_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

        //Lookup Inventory
        private void btnfind_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (ViewModel.MutasiWarehouse.FromWarehouse == null)
            {
                MessageBox.Show("Warehouse belum dipilih!");
                return;
            }
            InventorySearch inventorySearch = new InventorySearch();
            inventorySearch.Closed += new EventHandler(inventorySearch_Closed);
            inventorySearch.Show();
        }


        //Search Inventory Finished
        void inventorySearch_Closed(object sender, EventArgs e)
        {
            var inv = (sender as InventorySearch).Inventory;
            var mutasiDetail = DataGrid1.SelectedItem as MutasiWarehouseDetailDTO;
            mutasiDetail.Inventory = inv;
            if (inv != null)
            {
                mutasiDetail.Panjang = inv.Panjang;
                mutasiDetail.Lebar = inv.Lebar;
                if (ViewModel.MutasiWarehouse.FromWarehouse != null)
                {
                    try
                    {
                        mutasiDetail.Sheet = inv.Warehouses.FirstOrDefault(
                       x => x.KodeWarehouse == ViewModel.MutasiWarehouse.FromWarehouse.KodeWarehouse).Sheet;
                        mutasiDetail.Qty = inv.Warehouses.FirstOrDefault(
                            x => x.KodeWarehouse == ViewModel.MutasiWarehouse.FromWarehouse.KodeWarehouse).Qty;
  
                    }catch(Exception ex)
                    {
                        MessageBox.Show("SerialNumber " + inv.SerialNumber  + " tidak berada pada " +
                                        ViewModel.MutasiWarehouse.FromWarehouse.WarehouseName);
                    }
                }
            }
            DataGrid1.Focus();
        }

        private void btnCloseStatus_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ViewModel.CloseStatus();
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	ViewModel.LoadDetailFromFile(null);
        }

        private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            //MutasiWarehouseDetailDTO mutasiDetail = (sender as TextBox).DataContext as MutasiWarehouseDetailDTO;
            //if (mutasiDetail != null)
            //    if (mutasiDetail.Inventory.Item.IsRounded)
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:N}", mutasiDetail.Qty);
            //    }
            //    else
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:0.0000}", mutasiDetail.Qty);
            //    }
        }
     

    }
}
